from datetime import datetime
from random import random, choice


class Shopper:
    def __init__(self, money, sc):
        self.money = money
        self.shopping_cart = sc

    def shop(self, this_mall):
        for pro_name, pro_price in this_mall.products_own.items():
            print(f"商品名称：{pro_name},价格：{pro_price}")
        product_name = input("请输入购买的商品名称：")
        this_mall.sale(self, product_name)

    def want_refund(self):
        if len(self.shopping_cart.pro_info_list) == 0:
            print("您购物车里没有已购入的商品")
            return
        product_name = input("请输入要退货的商品名称：")
        Mall.refund(self, product_name)


class ShoppingCart:
    def __init__(self, pro_info_list, pro_sum, pro_count):
        # 购物信息列表
        self.pro_info_list = pro_info_list
        # 购买总商品的价值
        self.pro_sum = pro_sum
        # 购买数量
        self.pro_count = pro_count


class Mall:
    def __init__(self, products_own):
        self.products_own = products_own

    def sale(self, shopper, product_name):
        products = self.products_own
        print(shopper.money)
        money = shopper.money
        if products.get(product_name) is None:
            print("没有这个商品，别瞎弄！")
            return
        product_price = float(products.get(product_name))
        if product_price > money:
            print("穷鬼，钱不够！请到其他超市购买！")
            return
        money -= product_price
        shopper.money = money
        current_datetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        product_info = ProductInfo(product_name, product_price, current_datetime)
        sc = shopper.shopping_cart
        sc.pro_info_list.append(product_info)
        sc.pro_sum += product_price
        sc.pro_count += 1
        shopper.shopping_cart = sc
        print(f"恭喜，购买成功！您的卡余额还剩{money}")

    @staticmethod
    def refund(shopper, product_name):
        # print(shopper)
        pil = shopper.shopping_cart.pro_info_list
        del_product = Mall.delete_product_by_name(pil, product_name)
        if del_product is None:
            print("退货失败，购物车里没有这个商品")
        price = del_product.product_price
        shopper.money += price
        shopper.shopping_cart = pil
        print("退货成功")

    @staticmethod
    def delete_product_by_name(pro_info_list, product_name):
        """
        通过商品名称删除商品，并返回被删除的商品信息。
        如果没有找到匹配的商品，返回 None。
        """
        for product in pro_info_list:
            if product.product_name == product_name:
                pro_info_list.remove(product)  # 从列表中删除该商品
                return product  # 返回被删除的商品信息
        return None  # 如果没有找到匹配的商品，返回 None

    @staticmethod
    def print_receipt(shopper):
        shopping_cart = shopper.shopping_cart
        print(f"卡余额：{shopper.money}")
        product_info_list = shopping_cart.pro_info_list
        if len(product_info_list) == 0:
            print("您未购入任何商品")
            print(f"卡余额：{shopper.money}")
            return
        print("购物清单如下：")

        fg = "*****************************************"
        print(fg)
        for product_info in product_info_list:
            print(product_info)
        print(fg)
        print(f"卡余额：{shopper.money}")
        print(f"购物数量:{shopping_cart.pro_count},商品总价值：{shopping_cart.pro_sum}")


# 购物信息
class ProductInfo:
    def __init__(self, product_name, product_price, shop_time):
        self.shop_time = shop_time
        self.product_name = product_name
        self.product_price = product_price

    def __str__(self):
        return f"**商品：{self.product_name}，价格：{self.product_price},购物时间：{self.shop_time}"


# 优惠卷
class Vouchers:
    def __init__(self, product_name, discount):
        self.product_name = product_name
        self.discount = discount

    def __str__(self):
        return f"优惠劵：(商品:{self.product_name},优惠力度：{self.discount * 10}折)"


products = {
    "苹果": 5.99,
    "香蕉": 2.49,
    "牛奶": 3.50,
    "面包": 4.25,
    "鸡蛋": 2.99,
    "巧克力": 7.99,
    "咖啡": 8.50,
    "薯片": 3.99,
    "橙汁": 4.75,
    "饼干": 6.42
}

if __name__ == '__main__':
    mall = Mall(products)
    shopping_cart = ShoppingCart(pro_info_list=[], pro_sum=0, pro_count=0)
    shopper = Shopper(20.0, shopping_cart)

    # 生成优惠劵
    keys_list = list(products.keys())
    vou_list = []
    for k in keys_list:
        d = round(random(), 1)
        v = Vouchers(k, d)
        vou_list.append(v)
    # 抽取优惠劵
    random_vou = choice(vou_list)
    print(random_vou)
    pn = random_vou.product_name
    products = mall.products_own
    price = products.get(pn)
    new_price = round(price * random_vou.discount, 2)
    products[pn] = new_price

    while True:
        print("***********************")
        print("**可选的操作如下:*********")
        print("*1. 退出购物（Q/q)*******")
        print("*2. 想要退货（TD)********")
        print("*3. 购物，按其他任意键*")
        print("***********************")
        option = input("请选择要进行的操作")
        if option == 'q' or option == 'Q':
            # 进行退出操作
            mall.print_receipt(shopper)
            break
        elif option == "TD":
            shopper.want_refund()
        else:
            shopper.shop(mall)
